package com.atguigu.es;

import com.atguigu.beans.Employee;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Bulk;
import io.searchbox.core.Delete;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Index;

import java.io.IOException;

public class WriteBulk {
    public static void main(String[] args) throws IOException {
        //一.创建和es数据库的连接,获取一个客户端

        //1. new出这个工厂
        JestClientFactory factory = new JestClientFactory();
        //2. 看源码这个工厂有连接es地址的默认属性,得主动为属性赋值,否则连不上
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();
        factory.setHttpClientConfig(httpClientConfig);
        //3.通过工厂得到jestclient
        JestClient client = factory.getObject();

        //二.中间操作

        //2.批量写,删除1000000,1000001,写入1000002
            //2.0 建造者建造删除对象
        Delete delete1 = new Delete.Builder("1000000").index("test").type("emps").build();
        Delete delete2 = new Delete.Builder("1000001").index("test").type("emps").build();
            //2.1使用beans封装json请求
        Employee employee = new Employee("1001", 20, 2000.0, "李三3", "男", "吃饭睡觉");
            //2.2.建造者建造写入对象
        Index write = new Index.Builder(employee).index("test").type("emps").id("1000002").build();
            //2.3 批量写
        Bulk bulk = new Bulk.Builder().addAction(delete1).addAction(delete2).addAction(write).build();
        //3.执行写入
       client.execute(bulk);

        //三.关闭客户端
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
